package br.eti.busarello;

import java.awt.BorderLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;

public class FrameTest extends JFrame {
	private static final long serialVersionUID = 1L;

	private ComboLookAndFeels comboLookAndFeels = new ComboLookAndFeels();
	private final JScrollPane scrollPane = new JScrollPane();
	private final JTable table = new JTable(5, 2);
	
	private TableColumnAdjuster tableColumnAdjuster = new TableColumnAdjuster(table);
	private final JButton btnNewButton = new JButton("New button");

	public FrameTest() {

		initComponents();
	}

	private void initComponents() {
		// setDefaultCloseOperation(EXIT_ON_CLOSE);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);

		setSize(400, 300);

		getContentPane().setLayout(new BorderLayout());

		getContentPane().add(comboLookAndFeels, BorderLayout.SOUTH);

		comboLookAndFeels.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println(comboLookAndFeels.getSelected());
				comboLookAndFeels.apllyLookAndFeel(FrameTest.this);
			}
		});

		getContentPane().add(this.scrollPane, BorderLayout.CENTER);

		//table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		
		this.scrollPane.setViewportView(this.table);

		LabelTableColumn labelTableColumn = LabelTableColumn.install(table, 1);
		
		labelTableColumn.setIcon(new ImageIcon(getClass().getResource("/exit.png")));
		this.btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tableColumnAdjuster.adjustColumns();
			}
		});
		
		
		getContentPane().add(this.btnNewButton, BorderLayout.NORTH);
	}

	@Override
	public void dispose() {
		if (canClose()) {
			super.dispose();
		}
	}

	private boolean canClose() {
		Window[] ownedWindows = getOwnedWindows();

		for (Window window : ownedWindows) {
			if (window.isVisible()) {
				return false;
			}
		}

		return true;
	}

	public static void main(String[] args) throws InterruptedException {

		new FrameTest().setVisible(true);
	}
}
